import { RouterView,createRouter, createWebHashHistory } from "vue-router";
import {h} from 'vue'
const router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {path:'/',redirect:'/login'},
        {path:'/login',component: () => import ('./components/login.vue')},
        {path:'/home' ,
        component: () => import ('./components/home.vue'),
        redirect:'/home/users',
        children:[{
            path:'users',
            component:{render:() => h(RouterView)},
            children:[
                {
                    path:'',
                    component: () => import ('./components/SubCom/MyUsers.vue')
                },
                {
                    path:':id',
                    name:'details',
                    component:() => import ('./components/user/MyUserDetail.vue'),props:true
                }
            ]
        },
        {
            path:'rights',
            component:() => import ('./components/SubCom/MyRights.vue')
        },
        {
            path:'goods',
            component:() => import ('./components/SubCom/MyGoods.vue')
        },
        {
            path:'orders',
            component:() => import ('./components/SubCom/MyOrders.vue')
        },
        {
            path:'settings',
            component:() => import ('./components/SubCom/MySettings.vue')
        },
        {
            path:'menu',
            component:() => import ('./components/SubCom/MyMenu.vue')
        },
    //     {
    //     path:'users/:id',
    //     component:() => import ('./components/user/MyUserDetail.vue'),props:true
    // }
    ]
    },
   
    ]
})
router.beforeEach((to,from,next) => {
    if(to.path === '/login'){
        return next()
    }
    const token = localStorage.getItem('token')
    if(!token){
        return next('/login')
    }
    next()
})
export default router;